<?php
class Staticizer_News_Content extends Staticizer
{
    public function __construct()
    {
        parent::__construct();
    }

    public function Build($param=NULL)
    {
        #验证
        if(gettype($param)!='array'){
            throw new Exception('error');
        }

        #取参       
        $action=ucfirst(strtolower($param['Action']));

        #响应动作
        if(method_exists($this,$methodName='Action_'.$action))
        {
      
            if($action=='Add'||$action=='Edit')
            {
                //获取数据
                $id=$param['ID'];               
                $data=DB::GetRowByID('news',$id);
                if(!$data){
                    throw new Exception('data.error');
                }
                $this->$methodName($data);
            }
            else{
                $this->$methodName($param['Data']);
            }
        }
        else{
            throw new Exception('error');
        }
    }
    
    private function Action_Add($data)
    {         
        #保存自己
        $data_action=$this->Execute($data);        
        $filePath=$this->Save('news/detail/'.$data['ID'].'.shtml',$data_action['HTMLFile_Content'],TRUE);   //分组法创建文件,并返回路径更新数据
        DB::UpdateRowByID('news',$data['ID'],array
        (
            'M_HTMLFile'=>$filePath,
            'M_HTMLFile_Related'=>$data_action['ID_Related']
        ));

        #先更新相关
        foreach(explode(',',$data_action['ID_Related']) as $value)
        {
            $data_row=DB::GetRowByID('news',$value);
            //它们重新生成自己并计算与自己相关的记录
            $data_temp=$this->Execute($data_row);
            //保留自己
            $this->Save('news/detail/'.$data_row['M_HTMLFile'],$data_temp['HTMLFile_Content']);
            //更新与自己相关的记录
            DB::UpdateRowByID('news',$value,array
            (
                'M_HTMLFile_Related'=>$data_temp['ID_Related']
            ));
        }        
    }
    
    private function Action_Edit($data)
    {
        #先更新相关，因为本记录中还保留着先前与之相关的记录ID
        foreach(explode(',',$data['M_HTMLFile_Related']) as $value)
        {
            $data_row=DB::GetRowByID('news',$value);
            $data_temp=$this->Execute($data_row);                                           //它们重新生成自己并计算与自己相关的记录
            $this->Save('news/detail/'.$data_row['M_HTMLFile'],$data_temp['HTMLFile_Content']);    //保留自己
            DB::UpdateRowByID('news',$value,array                                           //更新与自己相关的记录
            (
                'M_HTMLFile_Related'=>$data_temp['ID_Related']
            ));
        }
        
        #后更新自己
        $data_action=$this->Execute($data);
        $this->Save('news/detail/'.$data['M_HTMLFile'],$data_action['HTMLFile_Content']);          //重新生成自己
        DB::UpdateRowByID('news',$data['ID'],array
        (
            'M_HTMLFile_Related'=>$data_action['ID_Related']
        ));        
    }

    private function Action_Delete($data)
    {
        //删除自己                
        @unlink(Config::PATH_AP_CONTENT_NEWS.'/detail/'.$data['M_HTMLFile']);

        //更新相关
        foreach(explode(',',$data['M_HTMLFile_Related']) as $value)
        {
            $data_row=DB::GetRowByID('news',$value);
            $data_temp=$this->Execute($data_row);
            $this->Save('news/detail/'.$data_row['M_HTMLFile'],$data_temp['HTMLFile_Content']);
            DB::UpdateRowByID('news',$value,array
            (
                'M_HTMLFile_Related'=>$data_temp['ID_Related']
            ));
        }
    }

    public function Execute($data)
    {
            #数据缓存
            $id_related=array();    //用于记录相关联的记录
           
            #处理数据
            //类别名
            $data['ClassName']=DAO_NewsClass::GetNameByID($data['ClassID']);
            //下一篇新闻
            $data['IsHaveNext']='FALSE';
            $data['Next_Title']=NULL;
            $data['Next_M_HTMLFile']=NULL;
            { 
                $data_next=db::GetRows('select ID,Title,M_HTMLFile from News where ClassID='.$data['ClassID'].' and ID<'.$data['ID'].' order by Datetime desc limit 0,1');
               
                if($data_next!=NULL)
                {
                    $data['IsHaveNext']='TRUE';
                    $data['Next_Title']=$data_next[0]['Title'];
                    $data['Next_M_HTMLFile']=Config::PATH_RELATIVE_CONTENT_NEWS.'/detail/'.$data_next[0]['M_HTMLFile'];
                    $id_related[]=$data_next[0]['ID'];
                }
            }            
            //上一篇新闻
            $data['IsHavePrevious']='FALSE';
            $data['Previous_Title']=NULL;
            $data['Previous_M_HTMLFile']=NULL;
            {
                $data_previous=db::GetRows('select ID,Title,M_HTMLFile from news where ClassID='.$data['ClassID'].' and ID>'.$data['ID'].' order by Datetime asc limit 0,1');

                if($data_previous!=NULL)
                {
                    $data['IsHavePrevious']='TRUE';
                    $data['Previous_Title']=$data_previous[0]['Title'];
                    $data['Previous_M_HTMLFile']=Config::PATH_RELATIVE_CONTENT_NEWS.'/detail/'.$data_previous[0]['M_HTMLFile'];
                    $id_related[]=$data_previous[0]['ID'];           
                }
            }            
            //相关新闻                        
            $data_related_keywords=DB::PGetRows("select ID,Title,Keywords,M_HTMLFile from news where ID<>? and concat(',',Keywords,',') regexp concat(',(',replace(?,',','|'),'),') order by Datetime desc",array
            (
                $data['ID'],
                $data['Keywords']
            ));
            if($data_related_keywords)
            {
                foreach($data_related_keywords as &$row)
                {
                    $row['M_HTMLFile']=Config::PATH_RELATIVE_CONTENT_NEWS.'/detail/'.$row['M_HTMLFile'];
                    $id_related[]=$row['ID'];
                }
                $id_related=array_unique($id_related);  //去掉重复的ID
            }
            
            #渲染模板
            View::Assign('News',$data);
            View::Assign('News_Keywords',explode(',',$data['Keywords']));
            View::Assign('News_Related',$data_related_keywords);
            
            return array('HTMLFile_Content'=>View::Fetch('news/content.tpl'),'ID_Related'=>join(',',$id_related));
    }
}
?>